package cn.codesugar.multimedia

import android.annotation.SuppressLint
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.SeekBar
import cn.codesugar.multimedia.databinding.ActivityPlayVideoTestBinding
import java.text.SimpleDateFormat
import java.util.*

class PlayVideoTestActivity : AppCompatActivity() {

    private var b: ActivityPlayVideoTestBinding? = null
    private var timer = Timer()
    private var duration = 0
    private var fromUser = false

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        b = ActivityPlayVideoTestBinding.inflate(layoutInflater)
        setContentView(b?.root)
        val uri = Uri.parse("android.resource://$packageName/${R.raw.g71daping}")
        // 设置要播放的视频文件的位置
        b?.videoView?.setVideoURI(uri)
        timer.schedule(timerTask, 1, 1)
        b?.videoView?.setOnPreparedListener {
            duration = b?.videoView?.duration ?: 0
            Log.e("20210610", "duration:$duration")
            b?.videoProgress?.max = duration
        }
        b?.videoInfo?.text = "0:00/" + duration.toLong().toDateStr()
        b?.play?.setOnClickListener {
            // 判断当前是否正在播放视频
            if (b?.videoView?.isPlaying == false) {
                // 开始或继续播放视频
                b?.videoView?.start()
            }
        }
        b?.videoProgress?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                Log.e("20210610", "onProgressChanged() progress:$progress, fromUser:$fromUser")
                this@PlayVideoTestActivity.fromUser = fromUser
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                Log.e("20210610", "onStartTrackingTouch()")
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                Log.e("20210610", "onStopTrackingTouch()")
                seekBar?.progress?.let { b?.videoView?.seekTo(it) }
                this@PlayVideoTestActivity.fromUser = false
            }

        })
        b?.pause?.setOnClickListener {
            if (b?.videoView?.isPlaying == true) {
                // 暂停播放视频
                b?.videoView?.pause()
            }
        }
        b?.replay?.setOnClickListener {
            // 将视频从头开始播放
            b?.videoView?.resume()
            if (b?.videoView?.isPlaying == false) b?.videoView?.start()
        }
    }

    /**
     * 时间戳转换成字符窜
     * @param pattern 时间样式 yyyy-MM-dd HH:mm:ss
     * @return [String] 时间字符串
     */
    @SuppressLint("SimpleDateFormat")
    fun Long.toDateStr(pattern: String = "mm:ss"): String {
        val date = Date(this)
        val format = SimpleDateFormat(pattern)
        return format.format(date)
    }

    private val timerTask = object : TimerTask() {
        @SuppressLint("SetTextI18n")
        override fun run() {
            val currentPosition = b?.videoView?.currentPosition
            runOnUiThread {
                currentPosition?.let {
                    b?.videoInfo?.text = currentPosition.toLong().toDateStr() + "/" + duration.toLong().toDateStr()
                    if (!fromUser) {
                        b?.videoProgress?.progress = currentPosition
                    }
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
        timer.purge()
        b?.videoView?.suspend()
    }
}